import pymysql

if __name__ == '__main__':
    db = pymysql.connect(host='127.0.0.1', user='root', passwd='wla', db='mydatabase', charset='utf8')
    # 创建游标对象
    cursor = db.cursor()
    # 执行SQL查询
    query = "SELECT * FROM stock_all"
    cursor.execute(query)
    result = cursor.fetchall()
    for row in result:
        # 获取需要操作的列的索引
        open = cursor.column_names.index('open')
        close = cursor.column_names.index('close')
        high = cursor.column_names.index('high')
        low = cursor.column_names.index('low')

        # 获取当前行的数据
        open_value = row[open]
        close_value = row[close]
        high_value = row[high]
        low_value = row[low]

        # 进行判断
        solid_len = abs(open_value - close_value)
        up_len = high_value - max(open_value, close_value)
        down_len = min(open, close_value) - low_value
        ratio = max(up_len / solid_len, down_len / solid_len)
        if (ratio > 5):
            is_long = 1
        else:
            is_long = 0

        # 更新数据库中的数据
        update_query = "UPDATE your_table SET column1 = %s, column2 = %s WHERE id = %s"
        update_values = (new_column1_value, new_column2_value, row[0])  # 假设id列为第一列
        cursor.execute(update_query, update_values)
        db.commit()

    # 关闭游标和连接
    cursor.close()
    db.close()